package sandbox;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

@SuppressWarnings("serial")
public class PersonnageEssai extends Component implements Runnable {

	BufferedImage img;
	List<BufferedImage> face;
	List<BufferedImage> profileG;
	List<BufferedImage> profileD;
	List<BufferedImage> derriere;
	private final int wref = 19;
	private final int href = 31;
	private final int wEspacement = 4;
	private int yTotal = 800;
	private int xTotal = 500;
	private int yCurrentPosition = 600;
	private int currentTabPos = 0;

	public PersonnageEssai(String fileName) throws Exception {
		URL url = this.getClass().getResource("img/" + fileName);
		img = ImageIO.read(url);
		face = new ArrayList<BufferedImage>(4);
		profileG = new ArrayList<BufferedImage>(4);
		profileD = new ArrayList<BufferedImage>(4);
		derriere = new ArrayList<BufferedImage>(4);
		int wdepart = 3;
		int hdepart = 0;
		System.out.println("width: " + img.getWidth(null) + " - height: "
				+ img.getHeight(null));
		for (int ii = 0; ii < 3; ii++) {
			derriere.add(img.getSubimage(wdepart, hdepart, wref, href));
			wdepart += wref + wEspacement;
		}
		wdepart = 3;
		hdepart += href+1;
		for (int ii = 0; ii < 3; ii++) {
			profileD.add(img.getSubimage(wdepart, hdepart, wref, href));
			wdepart += wref + wEspacement;
		}
		wdepart = 3;
		for (int ii = 0; ii < 3; ii++) {;
			face.add(img.getSubimage(wdepart, hdepart, wref, href));
			wdepart += wref + wEspacement;
		}
		wdepart = 3;
		for (int ii = 0; ii < 3; ii++) {
			derriere.add(img.getSubimage(wdepart, hdepart, wref, href));
			wdepart += wref + wEspacement;
		}
	}

	public Dimension getPreferredSize() {
		return new Dimension(xTotal, yTotal);
	}

	public void paint(Graphics g) {
		super.paint(g);
		g.drawImage(derriere.get(currentTabPos % 3), 200, yCurrentPosition,
				null);
		System.out.println("currentTabPos: "+currentTabPos+ " - yCurrentPosition: "+yCurrentPosition);
		++currentTabPos;
	}

	@Override
	public void run() {
		while (yCurrentPosition > 50) {
			repaint();
			yCurrentPosition -= 3;
			try {
				Thread.sleep(190);
			} catch (InterruptedException e) {

			}
		}

	}
}
